﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using System.Web.Security;
using makitRS.Core.Account;
using makitRS.WebUI.Models;

namespace makitRS.WebUI.Controllers
{
    public class AccountController : Controller
    {

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Login(string returnUrl)
        {
            var response = OpenIDManager.OpenIDProv.GetResponse();

            if (response == null)
            {
                try
                {
                    return OpenIDManager.OpenIDProv.CreateRequest("https://www.google.com/accounts/o8/id").RedirectingResponse.AsActionResult();
                }
                catch (ProtocolException ex)
                {
                    //TODO: Deal with error

                    ViewData["Message"] = ex.Message;
                    return View("LoginError");
                }
            }
            else
            {
                OpenIDManager.OpenIDResponseReturn res = OpenIDManager.parseResponse(response);

                if (res.ErrorOccured)
                {
                    //TODO: Deal with error

                    ViewData["Message"] = res.ErrorMessage;
                    return View("LoginError");
                }
                else if (res.UnknownUser)
                {
                    // Authed fine but this user is a new one so we need to register them with our app
                    return RedirectToAction("UnknownUser",new {id=res.UnknownUserClaimedID});
                }
                else
                {
                    // Authed fine so complete the forms auth and redirect back to the site content

                    FormsAuthentication.SetAuthCookie(res.UserName, false);

                    if (!string.IsNullOrEmpty(returnUrl))
                        return Redirect(returnUrl);
                    else
                        return RedirectToAction("Index", "Home");

                }
            }

        }

        [HttpGet]
        public ActionResult UnknownUser(string id)
        {
            UserAccount mdl = new UserAccount();
            mdl.ClaimedID = id;

            return View("UnknownUser", mdl);
        }

        [HttpPost]
        public ActionResult UnknownUser(UserAccount mdl)
        {
            // User has attempted to submit request

            if (ModelState.IsValid)
            {
                // Valid data so put request in queue for admin and alert as appropriate

                AccountManager.submitAccessRequest(mdl.UserName, mdl.EmailAddress, mdl.ClaimedID);

                return View("AccountAccessSubmitted", mdl);
            }

            return View(mdl);
        }
    }
}
